#include "metadata.h"

// define all the constructors
metadata::metadata()
{}

metadata::metadata(char *buffer,uint32_t metadataLen,char *fileID_)
{
	/*uint16_t filenameLen=0;
	uint32_t _filesize=0,keywordsLen=0;
	memcpy(&filenameLen,buffer,sizeof(uint16_t));
	char *_filename = (char *)malloc(filenameLen*sizeof(char));
	char *_sha1 = (char *)malloc(20*sizeof(char));
	char *_nonce = (char *)malloc(20*sizeof(char));
	char *bitVectorChr = (char *)malloc(1024*sizeof(char));
	if(_filename==NULL||_sha1==NULL||_nonce==NULL||bitVectorChr==NULL){
		   log_file->Writen("**malloc failed");
		   exit(1);
	}
	memset(_filename,0,filenameLen);
	memset(_sha1,0,20);
	memset(_nonce,0,20);
	memset(bitVectorChr,0,1024);
	//printf("\n in metadata constructor the filenamelen is %hu\n",filenameLen);

	memcpy(_filename,&buffer[2],filenameLen);
	memcpy(&_filesize,&buffer[2+filenameLen],sizeof(uint32_t));
	memcpy(_sha1,&buffer[2+filenameLen+4],20);
	memcpy(_nonce,&buffer[2+filenameLen+4+20],20);
	memcpy(&keywordsLen,&buffer[2+filenameLen+4+20+20],sizeof(uint32_t));
	char *unprocessedKeywords= (char *)malloc(sizeof(char )*keywordsLen);
	if(unprocessedKeywords == NULL){log_file->Write("**malloc failed");exit(1);}
	memset(unprocessedKeywords,0,keywordsLen);
	memcpy(unprocessedKeywords,&buffer[2+filenameLen+4+20+20+4],keywordsLen);
	//printf("\nunprocessed kw : %s.\n",unprocessedKeywords);
	memcpy(bitVectorChr,&buffer[2+filenameLen+4+20+20+4+keywordsLen],1024);

	//create Metadata object , Fileinfo object and to the map of files.
//	       Metadata m(std::string(filename,filenameLen),filesize,SHA1,nonce,unprocessedKeywords,keywordsLen,bitVectorChr);
	sha1 = (char *)malloc(sizeof(char)*20);
	nonce = (char *)malloc(sizeof(char)*20);
	fileID = std::string(fileID_,20);
	if(sha1==NULL||nonce==NULL){
		log_file->Writen("**malloc failed");
		exit(1);
	}
	filename=std::string(_filename,filenameLen);
	fileSize = _filesize;
	memcpy(sha1,_sha1,20);
	memcpy(nonce,_nonce,20);

	//process the keywords and bit vector
	uint32_t kwLen=keywordsLen;
	char *kwCopy=(char *)malloc((kwLen+1)*sizeof(char));
	if(kwCopy==NULL){
		log_file->Writen("**malloc failed");
		exit(1);
	}
	memset(kwCopy,0,kwLen+1);
	memcpy(kwCopy,unprocessedKeywords,kwLen);

	std::list<string> _kw;
	char *pch;
	pch=strtok(kwCopy," ");
	while(pch!=NULL){
		_kw.push_back(std::string(pch));
		pch= strtok(NULL," ");
	}
	keywords=_kw;

	bitset<1024> tmp(std::string((char*)bitVectorChr,1024));
	bitVector=tmp;
	free(kwCopy);
	free(_filename);
	free(_sha1);
	free(_nonce);
	free(bitVectorChr);
	free(unprocessedKeywords);*/
}



metadata::metadata(string _filename,uint32_t _filesize,char *_sha1,char *_nonce,char* _kwChr,uint32_t kwLen,char* _bitvectorStr,char *fileID_)
{
	/*sha1 = (char *)malloc(sizeof(char)*20);
	nonce = (char *)malloc(sizeof(char)*20);
	if(sha1==NULL||nonce==NULL){
		log_file->Writen("**malloc failed");
		exit(1);
	}
	fileID=std::string(fileID_,20);
	filename=_filename;
	fileSize = _filesize;
	memcpy(sha1,_sha1,20);
	memcpy(nonce,_nonce,20);

	//process the keywords and bit vector
	char *kwCopy=(char *)malloc((kwLen+1)*sizeof(char));
	if(kwCopy==NULL){
		log_file->Writen("**malloc failed");
		exit(1);
	}
	memset(kwCopy,0,kwLen+1);
	memcpy(kwCopy,_kwChr,kwLen);
	std::list<string> _kw;
	char *pch;
	pch=strtok(kwCopy," ");
	while(pch!=NULL){
		_kw.push_back(std::string(pch));
		pch= strtok(NULL," ");
	}
	keywords=_kw;

	bitset<1024> tmp(std::string((char*)_bitvectorStr,1024));
	bitVector=tmp;
	free(kwCopy);*/
}

metadata::metadata(string filename1,uint32_t filesize1,char *sha11,char *nonce1,list<string> keywords1, bitset<1024> bitvector1,char *fileID1)
{
	sha1 = (char *)malloc(sizeof(char)*20);
	nonce = (char *)malloc(sizeof(char)*20);
	fileID = (char *)malloc(sizeof(char)*20);
	if(sha1==NULL || nonce==NULL || fileID==NULL)
	{
		logFile->WriteFilen("Error : malloc failure in metadata.cpp\n");
		exit(1);
	}

	filename=filename1;
	fileSize = filesize1;
	memcpy(sha1,sha11,20);
	memcpy(nonce,nonce1,20);
	keywords=keywords1;
	bitVector=bitvector1;
	memcpy(fileID,fileID1,20);
}

metadata::metadata(metadata& md)
{
	/*fileID = md.fileID;
	filename=md.filename;
	fileSize=md.fileSize;
	sha1=(char *)malloc(20*sizeof(char));
	nonce=(char *)malloc(20*sizeof(char));
	if(sha1==NULL||nonce==NULL){
		log_file->Writen("**malloc failed");
		exit(1);
	}
	memcpy(sha1,md.sha1,20);
	memcpy(nonce,md.nonce,20);
	keywords=md.keywords;
	bitVector=md.bitVector;*/
}




string metadata::getFileID()
{
	return fileID;
}


uint32_t metadata::getFileSize()
{
    return fileSize;
}

string metadata::getFilename()
{
    return filename;
}

char *metadata::getNonce()
{
    return nonce;
}

char *metadata::getSha1()
{
    return sha1;
}

list<string> metadata::getKeywords()
{
    return keywords;
}

bitset<1024> metadata::getBitVector()
{
    return bitVector;
}

string convertToHex(char *binary)
{
	char *finalHex=(char *)malloc(40*sizeof(char));
	for (int i=0; i < 20 ; i++) 
	{
		sprintf(&finalHex[2*i],"%02x",(unsigned char )binary[i]);
	}
	string hex(finalHex,40);
	free(finalHex);
	return hex;

}

/*uint32_t Metadata::getMetadataLen()
{
	uint32_t size=0;
	size+=filename.size()+4+40+1024+getKeywordsLength();
	return size;
}

uint32_t Metadata::getKeywordsLength()
{
	uint32_t length=0;
	list<string>::iterator it;
	for(it=keywords.begin();it!=keywords.end();it++){
		length += (*it).size();
	}
	return length;
}

char * Metadata::getKeywordsChr(char *kw)
{
	int index=0;
	list<string>::iterator it;
	for(it=keywords.begin();it!=keywords.end();it++){
		memcpy(&kw[index],(*it).c_str(),(*it).size());
		index += (*it).size();
		kw[index++]=' ';
	}
	return kw;
}

string Metadata::getSHA1Hex()
{
	char *formatted=(char *)malloc(40*sizeof(char));

	for (int i=0; i < 20 ; i++) {
		sprintf(&formatted[2*i],"%02x",(unsigned char )sha1[i]);
	}
	string hex(formatted,40);
	free(formatted);
	return hex;
}
string Metadata::getNonceHex()
{
	char *formatted=(char *)malloc(40*sizeof(char));

	for (int i=0; i < 20 ; i++) {
		sprintf(&formatted[2*i],"%02x",(unsigned char )nonce[i]);
	}
	string hex(formatted,40);
	free(formatted);
	return hex;
}*/







